package cn.tanghom.framework.system.service.impl;

import java.util.ArrayList;
import java.util.List;

/**
 *接口实现
 */
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.tanghom.framework.support.tree.SelectTreeUtil;
import cn.tanghom.framework.support.tree.TreeObject;
import cn.tanghom.framework.system.dao.OrganizationDao;
import cn.tanghom.framework.system.entity.Organization;
import cn.tanghom.framework.system.service.OrganizationService;

@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Resource
    private OrganizationDao organizationDao;

    @Override
    public List GetParentsIdQueryChildren(String parentsId) {
        // TODO Auto-generated method stub
        return organizationDao.GetParentsIdQueryChildren(parentsId);
    }

    @Override
    public List GetParentsIdQueryChildrenParentsExsit(String parentsId) {
        // TODO Auto-generated method stub
        return organizationDao.GetParentsIdQueryChildrenParentsExsit(parentsId);
    }

    @Override
    public List GetChildrenQueryParentsId(String childId) {
        // TODO Auto-generated method stub
        return organizationDao.GetChildrenQueryParentsId(childId);
    }

    @Override
    public boolean add(Organization organization) {
        boolean result=false;
        try{
            organizationDao.add(organization);   
            result=true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;

    }

    @Override
    public List<Organization> findTreeList() {
        // TODO Auto-generated method stub
        return organizationDao.findTreeList();
    }

    @Override
    public boolean modify(Organization organization) {
        boolean result=false;
        try{
            organizationDao.update(organization);   
            result=true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;

    }

    @Override
    public Organization findById(Long Id) {
        // TODO Auto-generated method stub
        return organizationDao.findById(Id);
    }

    @Override
    public boolean delete(Organization organization) {
        boolean result=false;
        try{
            organizationDao.delete(organization);   
            result=true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

	@Override
	public List<TreeObject> findToSelectTree() {
		List<Organization> organizations = organizationDao.findAll();
		List<TreeObject> list = new ArrayList<TreeObject>();
		for (Organization org : organizations) {
			TreeObject ts = new TreeObject();
			ts.setId(org.getId().intValue());
			ts.setName(org.getName());
			ts.setParentId(org.getPid().intValue());
			list.add(ts);
		}
		SelectTreeUtil selectTreeUtil = new SelectTreeUtil();
		List<TreeObject> orgTree = selectTreeUtil.listToTree(list, 0);
		return orgTree;
	}

    @Override
    public List<Organization> findByIds(String ids) {
        String hql="from Organization o where o.id in ("+ids+")";
        return organizationDao.findList(hql);
    }

}
